package com.qf.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsRoleMapper;
import com.qf.pojo.DtsRole;
import com.qf.service.DtsRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author ZhangYuJie
 * projectName:shop_manage
 * time:2022/5/16 16:58
 * description:
 */
@Service
public class DtsRoleServiceImpl implements DtsRoleService {
    @Autowired
    DtsRoleMapper roleMapper;

    LambdaQueryWrapper<DtsRole> qw = new LambdaQueryWrapper<>();
    @Override
    public Set<String> findRoleByIds(Integer[] roleIds) {
        qw.in(DtsRole::getId, roleIds);
        List<DtsRole> roles = roleMapper.selectList(qw);

        //stream 转换为流， map： 要映射成一个什么样的对象  collect：指定返回的集合类型
        Set<String> roleStrs = roles.stream().map(role -> {
            return role.getName();
        }).collect(Collectors.toSet());

        return roleStrs;
    }

    @Override
    public Set<String> queryByIds(Integer[] roleIds) {
        Set<String> roles = new HashSet<String>();
        if (roleIds == null || roleIds.length == 0) {
            return roles;
        }

        //设置查询条件
        QueryWrapper<DtsRole> wapper = new QueryWrapper<>();
        wapper.in("id", Arrays.asList(roleIds));
        wapper.eq("deleted", false);
        wapper.eq("enabled", true);

        //查询
        List<DtsRole> roleList = roleMapper.selectList(wapper);

        for (DtsRole role : roleList) {
            roles.add(role.getName());
        }

        return roles;
    }

    @Override
    public IPage<DtsRole> querySelective(String name, Integer page, Integer size, String sort, String order) {
        IPage<DtsRole> iPage = new Page<>(page, size);

        //设置查询条件
        QueryWrapper<DtsRole> wapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(name)) {
            wapper.like("name", name);
        }
        wapper.eq("deleted", false);

        //设置排序
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            if (order.equalsIgnoreCase("asc")) {
                wapper.orderByAsc(sort);
            } else {
                wapper.orderByDesc(sort);
            }

        }

        //分页查询
        iPage = roleMapper.selectPage(iPage, wapper);

        return iPage;
    }

    @Override
    public List<DtsRole> queryAll() {
        QueryWrapper<DtsRole> wapper = new QueryWrapper<>();
        wapper.eq("deleted", false);
        return roleMapper.selectList(wapper);
    }

    @Override
    public DtsRole findById(Integer id) {
        return roleMapper.selectById(id);
    }

    @Override
    public boolean checkExist(String name) {
        QueryWrapper<DtsRole> wapper = new QueryWrapper<>();
        wapper.eq("deleted", false);
        wapper.eq("name", name);
        Integer count = roleMapper.selectCount(wapper);
        return count != 0;
    }

    @Override
    public void add(DtsRole role) {
        role.setAddTime(new Date());
        role.setUpdateTime(new Date());
        roleMapper.insert(role);
    }

    @Override
    public void updateById(DtsRole role) {
        role.setUpdateTime(new Date());
        roleMapper.updateById(role);
    }

    @Override
    public void deleteById(Integer id) {
        roleMapper.deleteById(id);
    }
}
